function onBridgeReady() {
    WeixinJSBridge.call('hideOptionMenu');
}

if (typeof WeixinJSBridge == "undefined") {
    if (document.addEventListener) {
        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
    } else if (document.attachEvent) {
        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
    }
} else {
    onBridgeReady();
}

$(function () {
    var isClick = true;
    var type = $(".redItem").data("type");
    var time = 60;
    // 用户领取红包类型切换
    $(".redItem").click(function () {
        $(this).find('img').addClass('active');
        $(this).siblings().find('img').removeClass('active');
        type = $(this).data("type");
    })
    var integration = $("#integration").val();
    var integralDiv = document.getElementById("integralId")
    integralDiv.innerHTML = "您当前剩余积分为：" + integration;
    var btn = document.getElementById("getRedPaper")
    var validateToken = null;
    var validateCode = null;
    var phone = null;
    var platform = null;
    var openId = null;
    if (integration < 10) {
        btn.innerHTML = "积分不足无法领取(点击充值)";
    } else {
        btn.innerHTML = "领取红包";
    }
    var url = null;

    // 用户点击领取红包按钮
    $('#getRedPaper').click(function () {
        if (!isClick) {
            return;
        }
        isClick = false;
        if (integration < 10) {
            window.location.href = $('#recharge_url').val()
            return;
        }
        phone = $("#accountInput").val();
        platform = $("#platform").val();
        openId = $("#openId").val();
        $(window).scrollTop(0) //点击按钮，页面回到顶部
        if (phone == null || phone == '' || phone.length < 11) {
            alert("手机号号码格式不正确哦");
            return;
        }
        btn.innerHTML = "请稍后...";
        if (type == 1 || type == 2) {
            url = "/spellLuck";
        } else if (type == 4) {
            url = "/activePacket";
        } else if (type == 5) {
            url = "/dayPacket";
        } else if (type == 6) {
            url = "/newActivity";
        } else if (type == 7) {
            url = "/generalLucky";
        } else if (type == 8 || type == 10) {
            url = "/pinzhi";
        }
        $.post(url, {
            'phone': phone,
            'platform': platform,
            'openId': openId,
            'type': type,
            'receiveType': 1
        }, function (data) {
            if (data.code == 401) {
                $(".mask").show();
                $('.time').hide()
                $(".times").show()
                $(".mask .tip").css("opacity", "0.68") //显示  短信验证码已发送，请查收 提示
                var timeOut = setInterval(function () {
                    if (time < 1) {
                        clearInterval(timeOut)
                        time = 60
                        $(".times").html('60s');
                        $(".time").show()
                        $(".times").hide()
                    } else {
                        time--;
                        $(".times").html(time + 's');
                    }
                }, 1000)
                validateToken = data.data;
                btn.innerHTML = "领取红包";
                isClick = true;
            } else if (data.code == 200) {
                if(platform == 11 || platform == 10 || platform == 7){
                    $("#mianfei").hide();
                }
                $(".getting").hide(); //领取页面隐藏，
                $(".getFail").hide();
                $(".rule").hide();
                $(".getted").show(); //领取成功页面显示
                $(".getSuccess").show();
                btn.innerHTML = "领取红包";
                var msg = data.data.split(",");
                var paperList = document.getElementById("paperListId");
                var html = '';
                for (var i = 0; i < msg.length; i++) {
                    html += '<div class="item">' + msg[i] + '</div>'
                }
                paperList.innerHTML = html;
                isClick = true;
            } else {
                if(platform == 11 || platform == 10 || platform == 7){
                    $("#mianfei").hide();
                }
                $(".getting").hide(); //领取页面隐藏，
                $(".rule").hide();
                $(".getted").show(); //领取成功页面显
                $(".getFail").show();
                btn.innerHTML = "领取红包";
                var msg = data.msg.split(",");
                var paperList = document.getElementById("contId");
                var html = '';
                for (var i = 0; i < msg.length; i++) {
                    html += '' + msg[i] + '' + '<br>';
                }
                paperList.innerHTML = html;
                isClick = true;

            }
        })
        //$(".getting").hide()    //领取页面隐藏，
        //$(".getted").show();    //领取成功页面显示
        // $(".getSuccess").show();   // 领取成功页面两种可能，一个有红包，一个上限
        // $(".getFail").show();
    })
    setTimeout(function () {
    }, 100);

    $('#mianfei').click(function () {
        window.location.href = 'http://t.cn/AidxOgLO';
    })

    // 用户点击换个手机号继续领红包
    $('#ContinueGetRed').click(function () {
        $(window).scrollTop(0) //点击按钮，页面回到顶部
        $(".getting").show() //领取页面显示，
        $(".rule").show();
        $(".getted").hide(); //领取成功页面隐藏

        $(".getSuccess").hide(); // 领取成功页面两种可能 隐藏不隐藏无所谓,看需求了
        // $(".getFail").hide();
        //$("#accountInput").val('') //input值 设置为空
    })

    // 弹框获取验证码
    $(".time").click(function () {
        $('.time').hide()
        $(".times").show()
        $(".mask .tip").css("opacity", "0.68") //显示  短信验证码已发送，请查收 提示
        var timeOut = setInterval(function () {
            if (time < 1) {
                clearInterval(timeOut)
                time = 60
                $(".times").html('60s');
                $(".time").show();
                $(".times").hide();
            } else {
                time--;
                $(".times").html(time + 's');
            }
        }, 1000)
    })

    // 关闭弹框
    $(".close").click(function () {
        $(".mask").hide()
        $(".mask .tip").css("opacity", "0") //关闭弹框隐藏  短信验证码已发送，请查收 提示
    })
    var isSureBtn = true;
    //提交验证码
    $(".sureBtn").click(function () {
        if (!isSureBtn) {
            return;
        }
        $(".mask").hide();
        isSureBtn = false;
        btn.innerHTML = "请稍后...";
        validateCode = $('#smsCode').val();
        if (type == 1 || type == 2) {
            url = "/spellLuck";
        } else if (type == 4) {
            url = "/activePacket";
        } else if (type == 5) {
            url = "/dayPacket";
        } else if (type == 6) {
            url = "/newActivity";
        } else if (type == 7) {
            url = "/generalLucky";
        } else if (type == 8 || type == 10) {
            url = "/pinzhi";
        }
        $.post(url, {
            'phone': phone,
            'platform': platform,
            'openId': openId,
            'type': type,
            'validateToken': validateToken,
            'validateCode': validateCode,
            'receiveType': 2
        }, function (data) {
            if (data.code == 200) {
                if(platform == 11 || platform == 10 || platform == 7){
                    $("#mianfei").hide();
                }
                $(".getting").hide(); //领取页面隐藏，
                $(".getFail").hide();
                $(".rule").hide();
                $(".getted").show(); //领取成功页面显示
                $(".getSuccess").show();
                btn.innerHTML = "领取红包";
                var msg = data.data.split(",");
                var paperList = document.getElementById("paperListId");
                var html = '';
                for (var i = 0; i < msg.length; i++) {
                    html += '<div class="item">' + msg[i] + '</div>'
                    paperList.innerHTML = html;
                }
                isSureBtn = true;
               /* if(platform != 11 && platform != 10 && platform != 7){
                    setTimeout("window.location.href='http://t.cn/AidxOgLO'",3000);
                }*/
            } else {
                if(platform == 11 || platform == 10 || platform == 7){
                    $("#mianfei").hide();
                }
                $(".getting").hide(); //领取页面隐藏，
                $(".rule").hide();
                $(".mask").hide();
                $(".getted").show(); //领取成功页面显
                $(".getFail").show();
                btn.innerHTML = "领取红包";
                var msg = data.msg.split("/");
                var paperList = document.getElementById("contId");
                var html = '';
                for (var i = 0; i < msg.length; i++) {
                    html += '' + msg[i] + '' + '<br>';
                    paperList.innerHTML = html;
                }
                isSureBtn = true;
            }
        })
    })

    var mySwiper = new Swiper('.swiper-container',{
        loop: true,
        autoplay: 2000,
    });

})

function mclear(){
    $("#accountInput").val("");
}

function huoqu(){
    $("#getRedPaper").trigger("click");
}




